package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.huiminpay.common.cache.util.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName : PayTestController
 只写这个即可
 */
@Slf4j
@Controller
public class PayTestController {
    String APP_ID = "9021000132694528";
    String APP_PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCM7iQraa+zMVb9i6rJzdq/MaAkJPpLb2Mw6uXMXXxTvKr7ClLYSlNdORTGQDHD8Wc59CjreldDi0JlZ9K2jyhaHt6HWrHxAnDI+y4TUnCGMAPMoAEA5+ORdj9GC7b33XZJYli4qgjccpbTifQig1Z6M4QuNq+Yscu1sXzyqoZkfqoMPEfKqK9GKmQjHo2bkM7vD/oWwkkvG8YstzoVNX9qD6IHFNsUafgcrVzQ+iYgLxrmgy/BxUmb8N1muCdxJ41Xq299oxPrV1Mk6MZ3uNHOJ7vrHvqQq7r2JUHCZ8fNmfSBAKPA+iofhro/hm5GI5jyQfklvBKbJ98A2fIOpae9AgMBAAECggEAFL86M+nrI8YvuEPHcM4NVfhISwnmjxRm1dy+18crdmhgA3URnSXT//VXSZhJuz7eo3l+TKxGDHuD2sneC2In3pyMxgBkEcb6M5xGvDF2v0pyRL2JaEi7B78QsY8n8HV55HPF++mMRGfaLGoytqH/KR5MZ0FFuIT0KFtouZDBGCoUlkIHdvReZCRnbTOVXbzvN6SNLZeL5TJLJfWDuK85eFBl2lsF7w5qXTKaJxV15CBYTXY4nQvXSjF+L+TBvlTd8C9CZb1tkC3jyTpUtoLdyOTJZ+Yp1namP8rWIhU+fzpw7fRBW/PjuE5Q4UqsKW9McDAfgXfSgEST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VTpgRyj87pnoiq8INV5cduphKw7ClRg07k7flvvF5E1X7soNXPuyyZpe8n8HO1hIpQklg5GjfgC+QacowTfreLTwU0JeglVwtQdt0VoAE06UPBrXOv+oZQIhnapf4CrZ1J1EQx67rUQpqX/JljeSwpxG/0UdTvRhUd984v3g4zIElXJpfXAwYtVe2MxQJZz0vP1/IOzDZk2upUD/bFMUhQIDAQAB";
    String charset = "utf-8";
    String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";//正式"https://openapi.alipay.com/gateway.do"

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID,
                                                            APP_PRIVATE_KEY, "json", charset, ALIPAY_PUBLIC_KEY, "RSA2");

        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();			//创建API对应的request

        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101124\"," +
                " \"total_amount\":\"0.01\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + charset);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    public static void main(String[] args) throws IOException {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        System.out.println(qrCodeUtil.createQRCode("http://192.168.1.25:56050/transaction/alipaytest",
                200, 200));
    }



}